﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using WindowsPhoneEssentials.ComponentModel;
using WindowsPhoneEssentials.Storage;
using WindowsPhoneEssentials.Testing.Abstractions;

namespace WindowsPhoneEssentials.Tests.Storage
{
    [TestClass]
    public class TransientStoreTests
    {
        private TransientStore applicationTransientStore;
        private FakePhoneApplicationService phoneApplicationService;

        [TestInitialize]
        public void Setup()
        {
            phoneApplicationService = new FakePhoneApplicationService();
            applicationTransientStore = new TransientStore(new MemoryIsolatedStorateFileFactory(), phoneApplicationService);
        }

        [TestMethod]
        public void can_save_and_restore_simple_type()
        {
            // arrange
            const string value = "test";

            // act
            applicationTransientStore.Save("key", value);

            // assert
            Assert.AreEqual("test", applicationTransientStore.Load<string>("key"));
        }

        [TestMethod]
        public void can_save_and_restore_simple_type_specifying_it_as_a_known_type()
        {
            // arrange
            const string value = "test";

            // act
            applicationTransientStore.Save("key", value);

            // assert
            Assert.AreEqual("test", applicationTransientStore.Load<string>("key"));
        }

        [TestMethod]
        public void can_save_and_restore_complex_type_that_needs_known_type()
        {
            // arrange
            var value = new Complex
                            {
                                Prop = new AnotherComplex
                                           {
                                               Simple = "test"
                                           }
                            };

            // act
            applicationTransientStore.Save("key", value);

            // assert
            Assert.AreEqual("test", ((AnotherComplex)applicationTransientStore.Load<Complex>("key").Prop).Simple);
        }

        public class Complex : NotifyPropertyChanged
        {
            public object Prop { get; set; }
        }

        public class AnotherComplex : NotifyPropertyChanged
        {
            public string Simple { get; set; }
        }
    }
}